package com.example.guethe.fireanalysisapp.activities;

import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;

import com.example.guethe.fireanalysisapp.R;
import com.example.guethe.fireanalysisapp.base.BaseActivity;
import com.example.guethe.fireanalysisapp.fragments.CatalogFragment;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.base.XFragmentAdapter;

public class HomeActivity extends BaseActivity{


    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    //顶栏
    @BindView(R.id.toolbar)
    public Toolbar toolbar;

    //打开侧边栏的图标
    @BindView(R.id.drawer_layout)
    public DrawerLayout drawer;

    //顶栏标题
    @BindView(R.id.tv_title)
    public TextView tv_title;

    List<Fragment> fragmentList = new ArrayList<>();
    String[] titles = {"告警", "正常", "离线"};

    XFragmentAdapter adapter;

    String Title="首页";


    //设置标题
    public void setTitle (String title) {
        if (this.tv_title == null) {
            this.tv_title = (TextView) findViewById(R.id.tv_title);
            if (this.tv_title == null) {
                return;
            }
        }
        this.tv_title.setText(title);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
    }


    @Override
    public void initData(Bundle savedInstanceState) {


        //点击打开侧边栏按钮的转动效果
        if(drawer==null||toolbar==null){
            this.drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            this.toolbar = (Toolbar) findViewById(R.id.toolbar);
            if (drawer==null||toolbar==null) {
                return;
            }
        }
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();




        super.initData(savedInstanceState);

        setTitle(Title);
        navigationView.setCheckedItem(R.id.nav_index);



        fragmentList.clear();
        fragmentList.add(CatalogFragment.newInstance());
        fragmentList.add(CatalogFragment.newInstance());
        fragmentList.add(CatalogFragment.newInstance());

        if (adapter == null) {
            adapter = new XFragmentAdapter(getSupportFragmentManager(), fragmentList, titles);
        }
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(3);

        tabLayout.setupWithViewPager(viewPager);

//        FragmentTransaction fragmentTransaction =
//                getSupportFragmentManager().beginTransaction();
//
//
//        CatalogFragment catalogFragment = new CatalogFragment();
//        fragmentTransaction.add(R.id.mainContent,catalogFragment);
//        fragmentTransaction.show(catalogFragment);
//        fragmentTransaction.commit();


    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    public Object newP() {
        return null;
    }
}
